package com.xr.controller;

import com.xr.model.Users;
import com.xr.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Zephyr.Liu
 * @date 2020/6/9 16:40
 * @Description
 */
@RestController
@RequestMapping("users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("login")
    public ResponseEntity login(@RequestBody Users users,HttpSession session){
        Users loginUser = usersService.login(users);
        Map<String,Object> map = new HashMap<>();
        if(loginUser!=null){
            map.put("msg","登录成功");
            map.put("loginUser",loginUser);
            map.put("token",session.getId());
            return ResponseEntity.status(HttpStatus.OK).body(map);
        }else{
            map.put("msg","登录失败，用户名或密码错误");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
        }
    }
}
